package com.hgsoft.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.hgsoft.dao.SideDoorPfDao;
import com.hgsoft.entity.SideDoorPf;
import com.hgsoft.util.DateUtil;
import com.hgsoft.util.Order;
import com.hgsoft.util.Pager;
import com.hgsoft.util.Property;

@Service
public class SideDoorPfService extends BaseService<SideDoorPf> {

	@Resource
	public void setDao(SideDoorPfDao dao) {
		super.setDao(dao);
	}

	@Override
	public SideDoorPfDao getDao() {
		return (SideDoorPfDao) super.getDao();
	}
	
	public List<Date> checkDates(String year) {
		List<Date> existDates = this.getDao().getExistDates(year);
		List<Date> dates;
		Date startDate = DateUtil.getDate("yyyy-MM-dd", year + "-01-01");
		Date endDate;
		Date today = new Date();
		String thisYear = DateUtil.getDateStr(today, "yyyy");
		if(thisYear.equals(year)) {
			endDate = today;
		} else {
			endDate = DateUtil.getLastDayByYear(startDate);
		}
		dates = DateUtil.getDates(startDate, endDate);
		dates.removeAll(existDates);
		return dates;
	}

	public void saveList(List<SideDoorPf> list) {
		for (SideDoorPf temp : list) {
			this.save(temp);
		}
	}

	public boolean findByDate(Date date, Integer line) {
		List<SideDoorPf> list = getDao().findAll(Order.desc("id"), Property.eq("date", date), Property.eq("line", line));
		if (list != null && !list.isEmpty()) {
			return true;
		} else {
			return false;
		}
	}

	/*
	 * @SuppressWarnings("rawtypes") public List<SideDoorPf> query(Pager pager,
	 * SideDoorPf query) {
	 * 
	 * List<SideDoorPf> sideDoorPfList = new LinkedList<SideDoorPf>(); if (query
	 * == null) { query = new SideDoorPf(); } List list =
	 * this.getDao().query(pager, query.getLine(), query.getDate(),
	 * query.getStation()); if (list != null) { for (Object o : list) {
	 * SideDoorPf sd = new SideDoorPf(); Object[] os = (Object[]) o; Integer id
	 * = (Integer) os[0]; Date date = (Date) os[1]; BigInteger line =
	 * (BigInteger) os[2]; String station = (String) os[3]; Integer num =
	 * (Integer) os[4]; sd.setId(id); sd.setDate(date);
	 * sd.setLine(line.intValue()); sd.setStation(station); sd.setNum(num);
	 * sideDoorPfList.add(sd); } } return sideDoorPfList; }
	 */

	public List<SideDoorPf> query(Pager pager, SideDoorPf query) {

		List<Property> properties = new ArrayList<Property>();

		if (query != null) {
			if (query.getDate() != null) {
				properties.add(Property.eq("date", query.getDate()));
			}
			if (query.getLine() != null) {
				properties.add(Property.eq("line", query.getLine()));
			}
			if (query.getStation() != null && !query.getStation().isEmpty()) {
				properties.add(Property.eq("station", query.getStation()));
			}
		}

		return getDao().findByPager(pager, new Order[] { Order.desc("id") }, properties.toArray(new Property[properties.size()]));
	}

	public void delete(Date date) {
		this.getDao().delete(date);
	}

}
